package club.dataresources.controller;

import club.dataresources.bo.BarberWorkTimeBo;
import club.dataresources.common.R;
import club.dataresources.entity.BarberWorkTime;
import club.dataresources.service.BarberWorkTimeService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author
 * @create 2018-11-12 0:42
 **/
@RequestMapping(value = "/admin/barberWorkTime")
@Controller
public class AdminBarberWorkTimeController {
    @Autowired
    private BarberWorkTimeService barberWorkTimeService;

    @RequestMapping("/addBarberWorkTime")
    public @ResponseBody
    R addBarberWorkTime(@RequestBody BarberWorkTime barberWorkTime){
        System.out.println("===================增加排班记录====================");
        try{
            if(barberWorkTimeService.addBarberWorkTime(barberWorkTime)){
                return R.ok().put("code",200).put("msg","增加成功");
            }else{
                return R.error().put("code",500).put("msg","增加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/removeBarberWorkTime")
    public @ResponseBody
    R removeBarberWorkTime(@RequestParam("barberId") Integer barberId){
        System.out.println("===================删除排班记录====================");
        try{
            if(barberWorkTimeService.removeBarberWorkTime(barberId)){
                return R.ok().put("code",200).put("msg","删除成功");
            }else{
                return R.error().put("code",500).put("msg","删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/updateBarberWorkTime")
    public @ResponseBody
    R updateBarberWorkTime(@RequestBody BarberWorkTime barberWorkTime){
        System.out.println("===================更新排班记录====================");
        try{
            if(barberWorkTimeService.updateBarberWorkTime(barberWorkTime)){
                return R.ok().put("code",200).put("msg","更新成功");
            }else{
                return R.error().put("code",500).put("msg","更新失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/findBwtByBarberId")
    public @ResponseBody
    R findBwtByBarberId(@RequestParam("barberId") Integer barberId){
        System.out.println("===================根据barberId查找排班记录====================");
        try{
            return R.ok().put("code",200).put("data",barberWorkTimeService.findBwtByBarberId(barberId)).put("msg","查找成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/listBarberWorkTime")
    public @ResponseBody
    R listBarberWorkTime(@RequestBody BarberWorkTimeBo barberWorkTimeBo){
        System.out.println("================查询显示排班记录================");
        try{
            Page<BarberWorkTime> liPage = barberWorkTimeService.listBarberWorkTime(barberWorkTimeBo.getPage(),barberWorkTimeBo.getRows(),barberWorkTimeBo);
            return R.ok().put("data",liPage.getResult()).put("code",200).put("msg","显示完毕").put("total",liPage.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }
}
